using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using OpenQuarters.WebQuarters.Core;
using OpenQuarters.WebQuarters.Modules.Core;
using OpenQuarters.WebQuarters.Utils;

namespace OpenQuarters.WebQuarters.Core.Web.Areas.Site.Controllers
{
    public class HomeController : Controller, IJavaScriptProvider
    {
        public virtual ActionResult Index(string id)
        {
            Initialise(id);

            ViewData.Model = CMSPage.Current;
            return View();
        }

        public CMSPageVersion Initialise(string id)
        {
            Oracle.CurrentControllerContext = this.ControllerContext;
            CMSPageVersion version = CMSPage.GetVersionForUser(id);

            if (version == null)
            {
                // No live page version exists for this page, 
                // and it does not belong to the current user
                Redirect(CMSPage.LoginPage.GetUrl());
            }

            CMSPage.Current = version;

            if (!Core.User.Current.HasPermission(Permissions.CMSPage.View, CMSPage.Current.PageId.ToString()))
            {
                if (CMSPage.Current.PageId == CMSPage.LoginPage.PageId)
                {
                    Permission loginPermission = Permissions.CMSPage.View;
                    loginPermission.Value = CMSPage.Current.PageId.ToString();
                    loginPermission.RoleId = Role.GuestRoleId;
                    loginPermission.Save();
                }
                else
                {
                    Core.User.Current.LastFailedURL = CMSPage.Current.CMSPage.GetUrl();
                    Redirect(CMSPage.LoginPage.GetUrl());
                }
            }

            if (CMSPage.Current.PageId == CMSPage.LoginPage.PageId)
            {
                int moduleCount = 0;
                CMSPage.Current.ContentDefinition.Areas.ForEach(a => moduleCount += a.Modules.Count);
                if (moduleCount == 0)
                {
                    CMSPage.Current.ContentDefinition["ContentArea.Default"].Modules.Add(new LoginModule());
                }
            }

            if (String.IsNullOrEmpty(CMSPage.Current.Template) || CMSPage.Current.Template == "Loading...")
            {
                CMSPage.Current.Template = "Default";
            }

            Core.CSS.ClearStyleSheets();
            Core.JavaScript.ClearScripts();

            if (Core.User.Current.HasPermission(Permissions.CMSPage.Edit, CMSPage.Current.PageId.ToString()))
            {
                //Register admin page scripts
                Core.JavaScript.RegisterScript("nicEdit.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.nicEdit.js", JavaScriptType.AdminScript);
                Core.JavaScript.RegisterScript("WebQuarters.editor.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.WebQuarters.editor.js", JavaScriptType.AdminScript);
                Core.JavaScript.RegisterScript("WebQuarters.admin.common.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.WebQuarters.admin.common.js", JavaScriptType.AdminScript);
                //Register admin startup scripts
                Core.JavaScript.RegisterScript("WebQuarters.editor.startup.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.WebQuarters.editor.startup.js", JavaScriptType.AdminStartupScript);
            }

            return CMSPage.Current;
        }

        #region IJavaScriptProvider Members

        public void RegisterJavaScripts()
        {
            Core.JavaScript.RegisterScript("jquery-1.3.2.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery-1.3.2.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery.json.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery.json.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("WebQuarters.common.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.WebQuarters.common.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery.ui.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery.ui.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery.easing.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery.easing.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery.base64.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery.base64.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery-jtemplates.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery-jtemplates.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery-jforms.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery-jforms.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery.simplemodal.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery.simplemodal.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery.scrollfollow.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery.scrollfollow.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery.tinysort.min.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery.tinysort.min.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery.selectboxes.min.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery.selectboxes.min.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("date.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.date.js", JavaScriptType.CommonScript);
            Core.JavaScript.RegisterScript("jquery.datepicker.js", "OpenQuarters.WebQuarters.Core.Web", "Areas.CMS.Content.Scripts.jquery.datepicker.js", JavaScriptType.CommonScript);
        }

        #endregion
    }
}
